package com.yzz.composeweatherproject

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.items
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
import com.yzz.composeweatherproject.activity.WeatherinDetailActivity
import com.yzz.composeweatherproject.entity.WeatherCityBean
import com.yzz.composeweatherproject.entity.WeatherProvinceBean
import com.yzz.composeweatherproject.ui.theme.ComposeWeatherProjectTheme
import com.yzz.composeweatherproject.viewmodel.DataViewModel

class MainActivity : ComponentActivity() {
    private val mDataViewModel by viewModels<DataViewModel>()

    @SuppressLint("CoroutineCreationDuringComposition")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            ComposeWeatherProjectTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    var isShowDetaile by remember { mutableStateOf(false) }
                    var data: ArrayList<WeatherCityBean>? by remember { mutableStateOf(null) }
                    if (isShowDetaile) {
                        cityView(this, data) {
                            isShowDetaile = false
                        }
                    } else {
                        Greeting(mDataViewModel) {
                            isShowDetaile = true
                            data = it
                        }
                    }

                }
            }
        }
        mDataViewModel.getWeatherData()

    }
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun cityView(
    activity: MainActivity,
    data: ArrayList<WeatherCityBean>?,
    onClick: () -> Unit
) {
    Column {
        TopAppBar {
            Button(onClick = onClick) {
                Text(text = "返回")
            }
        }
        LazyVerticalGrid(columns = GridCells.Fixed(5)) {
            if (data != null)
                items(data) {
                    Text(text = it.name,
                        Modifier
                            .padding(10.dp)
                            .clickable {
                                val intent = Intent(activity, WeatherinDetailActivity::class.java)
                                val bundle = Bundle()
                                bundle.putSerializable("data", it)
                                intent.putExtra("bundle", bundle)
                                activity.startActivity(intent)
                            })
                }
        }
    }

}

@OptIn(ExperimentalFoundationApi::class, ExperimentalMaterialApi::class)
@Composable
fun Greeting(viewModel: DataViewModel, onItemCLick: (ArrayList<WeatherCityBean>?) -> Unit) {

    val stringData = viewModel.dataStringFlow.collectAsState()
    val list = stringData.value
    if (list != null) {
        LazyVerticalGrid(columns = GridCells.Fixed(5)) {
            items(list) {
                provinceList(it, onItemCLick)
            }
        }
    }
}

@ExperimentalMaterialApi
@Composable
private fun provinceList(
    bean: WeatherProvinceBean?,
    onItemCLick: (ArrayList<WeatherCityBean>?) -> Unit
) {
    ConstraintLayout() {
        val text = createRef()
        bean?.provinceName?.let {
            Text(text = bean.provinceName, modifier = Modifier
                .clickable {
                    onItemCLick.invoke(bean.cityNameList)
                }
                .padding(20.dp)
                .constrainAs(text) {
                    centerHorizontallyTo(parent)
                    centerVerticallyTo(parent)
                })
        }

    }
}

private fun changeData() {

}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    ComposeWeatherProjectTheme {
//        Greeting()
    }
}